<template>
    <div class="login">
        <!-- 导航 -->
        <div class="nav-bar">
            <van-nav-bar>
                <template #left>
                    <div class="nav-logo">
                        <div class=""><img src="../assets/images/home_active.png" alt=""></div>
                        <div>Luckin&nbsp;Coffee</div>
                    </div>
                </template>
                 <template #right>
                     <div class="nav-right" @click="goback">先逛一逛</div>
                </template>
            </van-nav-bar>
        </div>
        <!-- 欢迎回来 -->
        <div class="welcome">
            <div class="ch-wel">感谢注册</div>
            <div class="en-wel">Please register your account</div>
        </div>
        <!-- 注册 -->
        <div class="form-box">
            <van-form @submit="register">
                     <van-field
                        v-model="userInfo.nikeName"
                        name="nickName"
                        label="昵称"
                        placeholder="昵称"
                        :rules="[{ required: true, message: '请填写昵称' },{pattern:nickNameReg,message:'昵称格式不正确'}]"
                    />
                    <van-field
                        v-model="userInfo.phone"
                        name="phone"
                        label="手机号"
                        placeholder="手机号"
                        :rules="[{ required: true, message: '请填写手机号' },{pattern:phoneReg,message:'手机号格式不正确'}]"
                    />
                    <van-field
                        v-model="userInfo.password"
                        :type="isOpen?'text':'password'"
                        name="password"
                        label="密码"
                        placeholder="密码"
                        :right-icon="isOpen?'eye-o':'closed-eye'"
                        @click-right-icon="togglePasswordStatus"
                        autocomplete="off"
                        :rules="[{ required: true, message: '请填写密码' },{pattern:passwordReg,message:'以字母开头的数字、字母、下划线组成6-16位密码'}]"
                    />
                    <div class="forgot-box">
                        <div class="forgot-text" @click="toLogin">
                            已有账号？返回登录
                        </div>
                    </div>
                    <div style="margin: 16px;">
                        <van-button round block color="#0C34BA" native-type="submit" >注&nbsp;&nbsp;&nbsp;册</van-button>
                    </div>
                     <!-- <div style="margin: 16px;">
                        <van-button round block native-type="button" >登&nbsp;&nbsp;&nbsp;录</van-button>
                    </div> -->
            </van-form>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                isOpen:false,
                 // 验证手机号
                phoneReg:/^1[3-9]\d{9}$/,
                // 验证密码（以字母开头的数字、字母、下划线组成6-16位密码）
                passwordReg:/^[A-Za-z]\w{5,15}$/,
                // 昵称验证（1-10位中英文数字组合）
                nickNameReg:/^[\u4e00-\u9fa5A-Za-z]{1,10}$/,
                userInfo:{
                    phone:'',
                    password:'',
                    nikeName:''
                }
            }
        },
        methods: {
            // 点击逛一逛
            goback(){
                console.log(11);
                this.$router.push({name:'Home'})
            },

            togglePasswordStatus(){
                this.isOpen=!this.isOpen
            },
            // 点击跳转回 注册页
            toLogin(){
                this.$router.push({name:'Login'})
            },
            // 注册
            register(userInfo){
                console.log('userInfo',userInfo);
                this.axios({
                    method:'post',
                    url:'/register',
                    data:userInfo,
                }).then(res=>{
                    console.log('res=>',res);
                    this.$toast(res.data.msg)
                    if(res.data.code==100){
                        setTimeout(()=>{
                            this.$router.push({name:'Login'})
                        },2000)
                    }
                }).catch(err=>{
                    console.log( 'err=>',err);
                })
            }
        },
    }
</script>

<style lang="less" scoped>
// 登录
.form-box{
    margin-top: 50px;
    // 忘记密码
    .forgot-box{
        margin-top: 10px;
        display: flex;
        .forgot-text{
            margin-left: auto;
            font-size: 14px;
            color: #666;
        }
    }
}
// 欢迎回来
.welcome{
    margin-top: 80px;
    padding: 10px;
    .ch-wel{
        font-size: 26px;
        color: #666;
        font-weight: bold;
    }
    .en-wel{
        font-size: 14px;
        color: #666;
        margin-top: 20px;
    }
}
// 导航
.nav-bar{
    .nav-logo{
        display: flex;
        align-items: center;
        div{
            flex: 1;
            font-size: 16px;
            color: #666;
            font-weight: bold;
            img{
                width: 40px;
                height: 40px;
                border-radius: 50%;
                margin-right: 10px;
            }
        }
    }
    .nav-right{
        color: #0C34BA;
        font-size: 16px;
        font-weight: bold;
    }
}
</style>